#!/bin/bash

case $1 in
    ip)
      curl ifconfig.me;
      echo "";
    ;;
    build)
      cd ~/opi/ord;
      cargo build --release;
    ;;
    lprivpn)
      echo '请确保vp里面的unset打开, export关闭';
      sleep 1;
      ls -l ~/opi/vpns/PrivateVPN
    ;;
    sprivpn)
      cd ~/opi/vpns/PrivateVPN/;
      sudo openvpn $2
    ;;
    name)
      sed -i "s/^REPORT_NAME.*\+=.*/REPORT_NAME=\"$2\"/" ~/opi/modules/brc20_index/.env
      sed -i "s/^REPORT_NAME.*\+=.*/REPORT_NAME=\"$2\"/" ~/opi/modules/bitmap_index/.env
      sed -i "s/^REPORT_NAME.*\+=.*/REPORT_NAME=\"$2\"/" ~/opi/modules/sns_index/.env
      sed -i "s/^REPORT_NAME.*\+=.*/REPORT_NAME=\"$2\"/" ~/opi/modules/pow20_index/.env
      sleep 1
      sh $0 sbrc;
      sh $0 ssns;
      sh $0 sbit;
      sh $0 spow;
    ;;
    spigvpn)
      sudo pigchacli
    ;;
    pbrc)
      tail -f -n 200  ~/opi/modules/brc20_index/log.log
    ;;
    sbrc)
      cd ~/opi/modules/brc20_index/;
      sh start.sh;
    ;;
    ebrc)
      vi ~/opi/modules/brc20_index/.env
    ;;
    ppow)
      tail -f -n 200  ~/opi/modules/pow20_index/log.log
    ;;
    spow)
      cd ~/opi/modules/pow20_index/;
      sh start.sh;
    ;;
    epow)
      vi ~/opi/modules/pow20_index/.env
    ;;
    pbit)
      tail -f -n 200  ~/opi/modules/bitmap_index/log.log
    ;;
    sbit)
      cd ~/opi/modules/bitmap_index/;
      sh start.sh;
    ;;
    ebit)
      vi ~/opi/modules/bitmap_index/.env
    ;;
    psns)
      tail -f -n 200  ~/opi/modules/sns_index/log.log
    ;;
    ssns)
      cd ~/opi/modules/sns_index/;
      sh start.sh;
    ;;
    esns)
      vi ~/opi/modules/sns_index/.env
    ;;
    pindex)
      tail -f -n 200   ~/opi/modules/main_index/log.log
    ;;
    sindex)
      cd ~/opi/modules/main_index/;
      sh start.sh;
    ;;
    sall)
      sh $0 sindex;
      sh $0 sbrc;
      sh $0 ssns;
      sh $0 sbit;
      sh $0 spow;
    ;;
    kill)
      # api
      proc_id=`ps -ef|grep -i  api.js|grep -v "grep"|awk '{print $2}'`
      if [ ! -z  $proc_id ];then
            kill $proc_id
            echo "api is killing ..."
      fi
        # index.js
      proc_id=`ps -ef|grep -i  index.js|grep -v "grep"|awk '{print $2}'`
      if [ ! -z  $proc_id ];then
            kill $proc_id
            echo "main index.js is killing ..."
      fi
        # brc20_index.py
      proc_id=`ps -ef|grep -i  brc20_index.py|grep -v "grep"|awk '{print $2}'`
      if [ ! -z  $proc_id ];then
            kill $proc_id
            echo "brc20_index is killing ..."
      fi
        # bitmap_index.py
      proc_id=`ps -ef|grep -i  bitmap_index.py|grep -v "grep"|awk '{print $2}'`
      if [ ! -z  $proc_id ];then
            kill $proc_id
            echo "bitmap_index is killing ..."
      fi
        # sns_index.py
      proc_id=`ps -ef|grep -i  sns_index.py|grep -v "grep"|awk '{print $2}'`
      if [ ! -z  $proc_id ];then
            kill $proc_id
            echo "sns_index is killing ..."
      fi
        # pow20_index.py
      proc_id=`ps -ef|grep -i  pow20_index.py|grep -v "grep"|awk '{print $2}'`
      if [ ! -z  $proc_id ];then
            kill $proc_id
            echo "pow20_index is killing ..."
      fi
      echo "..."
      sleep 1
      sh $0 check
    ;;
    check)
      canShutdown=1
        # api
      proc_id=`ps -ef|grep -i  api.js|grep -v "grep"|awk '{print $2}'`
      if [ ! -z  $proc_id ];then
          canShutdown=0
          echo "api is running"
      fi
        # index.js
      proc_id=`ps -ef|grep -i  index.js|grep -v "grep"|awk '{print $2}'`
      if [ ! -z  $proc_id ];then
          canShutdown=0
          echo "index.js is running"
      fi
        # brc20_index.py
      proc_id=`ps -ef|grep -i  brc20_index.py|grep -v "grep"|awk '{print $2}'`
      if [ ! -z  $proc_id ];then
          canShutdown=0
          echo "brc20_index is running"
      fi
        # bitmap_index.py
      proc_id=`ps -ef|grep -i  bitmap_index.py|grep -v "grep"|awk '{print $2}'`
      if [ ! -z  $proc_id ];then
          canShutdown=0
          echo "bitmap_index is running"
      fi
        # sns_index.py
      proc_id=`ps -ef|grep -i  sns_index.py|grep -v "grep"|awk '{print $2}'`
      if [ ! -z  $proc_id ];then
          canShutdown=0
          echo "sns_index is running"
      fi
        # pow20_index.py
      proc_id=`ps -ef|grep -i  pow20_index.py|grep -v "grep"|awk '{print $2}'`
      if [ ! -z  $proc_id ];then
          canShutdown=0
          echo "pow20_index is running"
      fi
        # ord
      proc_id=`ps -ef|grep -i  "/bin/sh -c ./ord" |grep -v "grep"|awk '{print $2}'`
      if [ ! -z  $proc_id ];then
          canShutdown=0
          echo "ord is running"
      fi
        # bitcoin
      proc_id=`ps -ef|grep -i  bitcoin |grep -v "grep"|grep -v "ord"|awk '{print $2}'`
      if [ ! -z  $proc_id ];then
          canShutdown=0
          echo "bitcoin is running"
      fi
      if [ "$canShutdown" = "1" ];then
          echo "All is closed, you can shutdown now"
      fi
    ;;

    *)
        sh $0 check
    ;;
esac